#ifndef QUIZDEMOWIDGET_H
#define QUIZDEMOWIDGET_H

#include "quizwidget.h"
#include <Phonon/VideoWidget>
#include <Phonon/MediaObject>
#include <QFrame>
#include <QList>
#include <QIcon>

/**
 * A quiz which allows the user to select the appropriate
 * <i>demonstrator video</i> corresponding to the shown phrase.
 *
 * The user is presented with options A, B, C and D. The video
 * for each is shown alongside the options sequentially.
 */
class QuizDemoWidget : public QuizWidget {
        Q_OBJECT

        // PUBLIC METHODS ////////////////////////////////////////////////
public:
        QuizDemoWidget(QWidget* parent = 0);
        /**
         * Destructor. Returns the phonon widget to the designated parent
         * if one had been assigned with setPhononWidget().
         */
        ~QuizDemoWidget();

        // override createButton() to display pictures instead of text
        virtual QAbstractButton* createButton(Phrase phrase, int index);

        // override to use grid layout instead of box layout
        virtual QLayout* createOptionsLayout(const QList<QAbstractButton*> options);

        /**
         * Give this widget a Phonon video widget which it can use
         * to display videos. The Phonon widget will be returned
         * to the given layout.
         * @param widget The video widget which will be used to display
         * the demonstrator videos.
         * @param layout The layout to which the video widget will
         * be returned once the quiz is over.
         * @param c The Phonon Media Object used to load and play the clip, which
         * is also borrowed from the main GUI.
         */
        void setPhononWidget(Phonon::VideoWidget* widget, QLayout* layout, Phonon::MediaObject* c);

        /**
         * Play the option videos to the learner. Options A through to option D
         * are played on the Phonon widget.
         * setPhononWidget() must have been called first.
         */
        void playOptions();

        /**
         * Have the quiz draw the given image in the bottom-right corner.
         * @param QImage img The image to draw
         */
        void setImage(const QImage img);

        // PRIVATE METHODS //////////////////////////////////////////////
private:
        /**
         * Load the video for the next option
         */
        void playNextPhrase();
protected:
        // POLYMORPHISM /////////////////////////////////////////////////
        /*
         * Paint a helpful message
         */
        virtual void paintEvent(QPaintEvent *event);

        // MEMBER VARIABLES /////////////////////////////////////////////
private:
        /**
         * Size at which the demonstrator video will be shown.
         */
        static const int VIDEO_SIZE = 192;

        /**
         * The video itself. We borrow this from the main GUI
         * in setPhononWidget() and
         * clear it when we're done in cleanup().
         */
        Phonon::MediaObject *clip;
        /**
         * The video widget used to display the instructor videos.
         * This is 'borrowed' from the outside world and returned when\
         * the quiz is over.
         */
        Phonon::VideoWidget *phononWidget;
        /**
         * Phonon widget will be returned to this layout when quiz done.
         */
        QLayout *returnLayout;
        /**
         * This layout provides a place for us to position the Phonon video widget.
         */
        QFrame *phononHook;
        /**
         * A, B, C, D options
         */
        QString letters;
        /**
         * The currently playing option video
         */
        QList<Phrase>::iterator currentPhrase;
        /**
         * The icon to draw in the bottom-left corner
         */
        QImage icon;

        // PROTECTED SLOTS /////////////////////////////////////////////
protected slots:
        /*
         * Invoked when the 'done' button is clicked.
         */
        virtual void cleanup();
        /*
         * Invoked when user selects answer.
         */
        virtual void answerSelected();

        // PRIVATE SLOTS ///////////////////////////////////////////////
private slots:
        /**
         * An option video has finished playing. Move onto the next video.
         */
        void videoFinished();

        /**
         * Phonon media object state has changed. Start playing the video
         * once it has finished loading.
         */
        void phononStateChanged(Phonon::State newState, Phonon::State oldState);

        /**
         * Stop playing the videos for the various options.
         */
        //void stopPlayingVideos();
};

#endif // QUIZDEMOWIDGET_H
